<script>
	
	global.token = function() {
		try {
			var token = uni.getStorageSync('token');
		} catch (e) {
	
		}
		if (token == '') {
			return "notoken";
		} else {
			return token;
		}
	}
	
	global.access_token = "none";
	
	var Hub = require('@/components/SignalRClient.js');
	global.hub = null;
	var CustomerRegisterCtol = null;
	function CustomerRegisterFunc(){
		hub.invoke("CustomerRegister").then((res)=>{
		    //res = JSON.parse(res);
			//console.log(res);
		});
	}
	
	global.InitSignalR = function(){
		global.hub  = new Hub.HubConnection();
		clearInterval(CustomerRegisterCtol);
		hub.start(global.baseUrl+"/chatHub",{
		    access_token:global.access_token
		});
		hub.onOpen = (res)=>{
		    console.log("连接已开启")
			//注册客户端connectionid
			CustomerRegisterFunc();
			CustomerRegisterCtol = setInterval(function(){
				CustomerRegisterFunc();
			},15000)
		};
		//连接关闭事件
		hub.onClose = (res)=>{
		    console.log("连接意外关闭");
			//window.location.reload()
			uni.showModal({
				title:"系统提示",
				showCancel:false,
				content:"连接出现错误！请刷新应用！",
				success() {
					window.location.reload();
				}
			})
			global.InitSignalR();
		};
		
		//通讯过程中的Error事件
		hub.onError = (res) =>{
		    uni.showModal({
		    	title:"系统提示",
				showCancel:false,
				content:"连接出现错误！请刷新应用！",
				success() {
					window.location.reload();
				}
		    })
		};
	}

import http from '@/common/requestConfig.js';
	
export default {
	components: {
		http
	},
	onLaunch: function() {
		// uni.redirectTo({
		// 	url:"/pages/login/index"
		// })
		//setTimeout(function(){
			this.LoginInfo();
		//}.bind(this),1000)
		
	},
	onShow: function() {
		//console.log('App Show');
	},
	onHide: function() {
		//console.log('App Hide');
	},
	methods:{
		LoginInfo(){
			let that = this;
			//uni.showLoading();
			http.post("customer/CreateSignalR", {}
			).then(res => {
				//uni.hideLoading();
				if(res.code == 1){
					global.access_token = res.data;
					//console.log(global.access_token);
					global.InitSignalR();
					
				}else{
					uni.redirectTo({
						url:"/pages/login/index"
					})
				}
			}).catch(res=>{
				console.log('throw error..');
				//that.LoginInfo();
			});
		}
	}
};
</script>

<style lang='scss'>
@import './cool/ui/assets/css/index.scss';
@import './assets/css/index.scss';
</style>
